<?php

class UserController extends Controller
{
	public function actionIndex()
	{
		$this->render('index');
	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
        public function actionGetContact()
        {
            try{
                $post = Yii::app()->request->rawBody;
                $json=json_decode($post);
                $connection=Yii::app()->db;   // assuming you have configured a "db" connection
                $query="SELECT fa_group.* FROM fa_user_group INNER JOIN fa_group ON  fa_user_group.group_id=fa_group.id
                WHERE fa_user_group.user_id=" . $json->user_id;
                $command=$connection->createCommand($query);
                $dataReader=$command->query();
                $rows_group = array();
                $rows_group_id=array();
                while(($row=$dataReader->read())!==false) {
                    $rows_group[]=$row;
                    $rows_group_id[]=$row["id"];
                }
                $str_id=json_encode($rows_group_id);
                $str_id=str_replace("[","(", $str_id);
                $str_id=str_replace("]",")", $str_id);
                $query="Select * from fa_user_group where group_id IN ".$str_id;
                $command=$connection->createCommand($query);
                $dataReader=$command->query();
                $rows_group_user = array();
                while(($row=$dataReader->read())!==false) {
                    $rows_group_user[]=$row;
                }
                
                $query="Select DISTINCT fa_user.* from fa_user_group INNER JOIN fa_user ON  fa_user_group.user_id=fa_user.id where group_id IN ".$str_id." AND id!=".$json->user_id;
                $command=$connection->createCommand($query);
                $dataReader=$command->query();
                $rows_user = array();
                while(($row=$dataReader->read())!==false) {
                    $rows_user[]=$row;
                }                
                $data=array("group"=>$rows_group,"user_group"=>$rows_group_user,"user"=>$rows_user);
                $resul=array('data'=>$data,'status'=>'ok');
                $string=json_encode($resul);
                echo $string;
            }catch(Exception $e){
                $resul=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                 echo $string=json_encode($resul);
            }
                           

        }
        
        public function actionUpdateLocation()
        {
            try{
                $post = Yii::app()->request->rawBody;
                $json=json_decode($post);
                $connection=Yii::app()->db;   // assuming you have configured a "db" connection
                $sql="UPDATE fa_user SET fa_user.lat=:lat,fa_user.lng=:lng,fa_user.location=:location,fa_user.country_code=:country_code,fa_user.last_time=:last_time WHERE fa_user.id=:id";                 
                $command=$connection->createCommand($sql);
                $command->bindParam(":lat",$json->lat,PDO::PARAM_STR);
                $command->bindParam(":lng",$json->lng,PDO::PARAM_STR);
                $command->bindParam(":location",$json->location,PDO::PARAM_STR);
                $command->bindParam(":country_code",$json->country_code,PDO::PARAM_STR);
                $last_time=time();
                $command->bindParam(":last_time",$last_time,PDO::PARAM_STR);                
                $command->bindParam(":id",$json->id,PDO::PARAM_INT);
                $row_count=$command->execute();
                $resul=array('data'=>$data,'status'=>'ok');
                $string=json_encode($resul);
                echo $string;
            }catch(Exception $e){
                $resul=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                 echo $string=json_encode($resul);
            }
                           

        }
                
}